package com.example.jwt.config;


import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoop;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.*;
import io.netty.util.CharsetUtil;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;

import java.net.InetAddress;
import java.util.Random;

public class NettyConfig {


    public static void main(String[] args) {

        EventLoopGroup bossEventLoopGroup = new NioEventLoopGroup();
        EventLoopGroup childEventLoopGroup = new NioEventLoopGroup();

        ServerBootstrap serverBootstrap = new ServerBootstrap();
        serverBootstrap.group(bossEventLoopGroup, childEventLoopGroup)
                .channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer() {
            @Override
            protected void initChannel(Channel ch) throws Exception {
                ChannelPipeline pipeline = ch.pipeline();
                pipeline.addLast(new HttpServerCodec());
                pipeline.addLast(new SimpleChannelInboundHandler() {
                    @Override
                    protected void messageReceived(ChannelHandlerContext ctx, Object msg) throws Exception {
                        ByteBuf byteBuf = Unpooled.copiedBuffer(new Random().nextLong()+"", CharsetUtil.UTF_8);
                        DefaultFullHttpResponse httpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, byteBuf);
                        httpResponse.headers().set(HttpHeaderNames.CONTENT_TYPE, "text_plain");
                        httpResponse.headers().set(HttpHeaderNames.CONTENT_LENGTH,byteBuf.readableBytes()+"");
                        ctx.writeAndFlush(httpResponse).addListener(new GenericFutureListener(){
                            @Override
                            public void operationComplete(Future future) throws Exception {
                                ChannelFuture channelFuture = (ChannelFuture) future;
                                boolean open = channelFuture.channel().isOpen();
                                System.out.println(open);
                            }
                        });
                    }
                });
            }
        });
        try {
            ChannelFuture channelFuture = serverBootstrap.bind(8889).sync();
            channelFuture.channel().closeFuture().sync().addListener(new GenericFutureListener() {

                @Override
                public void operationComplete(Future future) throws Exception {
                    Object o = future.get();
                    System.out.println(o);
                }
            });
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            bossEventLoopGroup.shutdownGracefully();
            childEventLoopGroup.shutdownGracefully();
        }
        System.out.println("启动完成");

    }


}
